package cn.edu.lingnan.service.book.impl;

import cn.edu.lingnan.bean.Book;
import cn.edu.lingnan.repository.BookPageSortRepository;
import cn.edu.lingnan.repository.BookRepository;
import cn.edu.lingnan.service.base.impl.BaseServiceImpl;
import cn.edu.lingnan.service.book.BookService;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;


@Service
public class BookServiceImpl implements BookService {

    //@Resource
    //JpaRepository repository;
    @Resource
    BookRepository bookRepository;

    @Override
    public Map<String, Object> page(int pageNum, int pageSize,String entity){

        Specification sp = new  Specification<Book>(){
            /**
             * toPredicate: 构造查询条件的主要方法
             *     Root: 表示可查询和操作实体对象的根，如果将实体对象比喻为表明，root则为表的字段
             *
             *     CriteriaQuery: 查询操作对象，它包含操作的各个组件部分：select、from、where、group by、order by
             *
             *     CriteriaBuilder：构造器对象，其实相当于条件或条件组件
             */

            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = null;
                //hasBlank: hutool 工具包中封装的方法
                if (!StrUtil.hasBlank(entity)){
                    predicate = criteriaBuilder.like(root.get("name").as(String.class),"%"+entity+"%");
                }
                return predicate;
            }
        };

        Map<String, Object> map = new HashMap<>();
        PageRequest pageRequest = PageRequest.of(pageNum -1, pageSize, Sort.by(Sort.Direction.DESC,"id"));
        Page<Book> page = bookRepository.findAll(sp,pageRequest);
        map.put("TotalElements",page.getTotalElements());
        map.put("pageContent",page.getContent());
        return map;
    }


    //@Override
    //public Map<String, Object> page(int pageNum, int pageSize, String entity) {
    //    HashMap<String, Object> map = new HashMap<>();
    //    PageRequest pageRequest = PageRequest.of(pageNum -1, pageSize);
    //    Example<String> example = Example.of(entity);
    //    Page<Book> page = repository.findAll(example,pageRequest);
    //    map.put("TotalElements",page.getTotalElements());
    //    map.put("pageContent",page.getContent());
    //    return map;
    //}

    @Override
    public Map<String, Object> page(int pageNum, int pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        PageRequest pageRequest = PageRequest.of(pageNum -1, pageSize);
        // Example<T> example = Example.of(entity);
        Page<Book> page = bookRepository.findAll(pageRequest);
        map.put("TotalElements",page.getTotalElements());
        map.put("pageContent",page.getContent());
        return map;
    }

    @Override
    public List<cn.edu.lingnan.bean.Book> selectAll() {
        return null;
    }

    @Override
    public Optional<cn.edu.lingnan.bean.Book> selectById(Object id) {
        return Optional.empty();
    }

    @Override
    public cn.edu.lingnan.bean.Book save(cn.edu.lingnan.bean.Book entity) {

        return bookRepository.save(entity);
    }

    @Override
    public List<cn.edu.lingnan.bean.Book> saveBatch(List<cn.edu.lingnan.bean.Book> entity) {
        return null;
    }

    @Override
    public void deleteById(int id) {
        bookRepository.deleteById(id);
    }

    @Override
    public void deleteBatch(String[] ids) {

        int ids1[] = new int[10];
        for (int i = 0; i< ids.length;i++){
            ids1[i] = Integer.parseInt(ids[i]) ;
        }

        bookRepository.deleteBatchById(ids1);

    }

    @Override
    @Transactional
    public cn.edu.lingnan.bean.Book update(cn.edu.lingnan.bean.Book entity, Object id) {
      int i =  bookRepository.updateBookById(entity.getName(),entity.getAuthor(), entity.getPrice(), entity.getSales(), entity.getStocks(),
                entity.getImgPath(), entity.getCreateTime(), entity.getId());
        System.out.println(i);
        return entity;
    }


    //@Resource
    //BookPageSortRepository bookPageSortRepository;
    //
    //
    //@Override
    //public Page<Book> selectFindAllPageInfo(Pageable pageable) {
    //    return bookPageSortRepository.findAll(pageable);
    //}
    //
    //@Override
    //public Book insertSave(Book book) {
    //    return bookPageSortRepository.save(book);
    //}
    //
    //@Override
    //public Book updateByPriMaryKey(Book book) {
    //    return bookPageSortRepository.save(book);
    //}
    //
    //@Override
    //public List<Book> selectObjectBean(Object object) {
    //    return null;
    //}
    //
    //@Override
    //public void deleteById(Integer id) {
    //    bookPageSortRepository.deleteById(id);
    //}
    //
    //@Override
    //public Book findById(Integer id) {
    //    return bookPageSortRepository.getOne(id);
    //}
}
